package com.ship.contract.feign;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ship.common.core.base.Constant;
import com.ship.common.core.utils.DateUtils;
import com.ship.common.core.utils.StringUtils;
import com.ship.contract.bean.ConShipping;
import com.ship.contract.bean.vo.ConShippingRent;
import com.ship.contract.bean.vo.HireTimeDetails;
import com.ship.contract.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author zhangyh
 */
@Api(tags = "合同服务Fegin远程调用客户端")
@RestController
@RequestMapping("/contractFeign")
public class ContractClient {

    @Autowired
    private KonstService konstService;

    @Autowired
    private ConShippingAgencyService conShippingAgencyService;

    @Autowired
    private ConThirdPartyMeasurementService conThirdPartyMeasurementService;

    @Autowired
    private ConRefuelingService conRefuelingService;

    @Autowired
    private ConInsurancePeopleService conInsurancePeopleService;

    @Autowired
    private ConLawyerService conLawyerService;

    @Autowired
    private ConBrokerService conBrokerService;

    @Autowired
    private ConInsuranceGoodsService conInsuranceGoodsService;

    @Autowired
    private ConShippingService conShippingService;

    @GetMapping(value = "/contractExpireMessageSend")
    public void contractExpireMessageSend() {
        //船代合同
        conShippingAgencyService.contractExpireMessageSend();
        //第三方计量合同
        conThirdPartyMeasurementService.contractExpireMessageSend();
        //加油合同
        conRefuelingService.contractExpireMessageSend();
        //租船人责任险合同
        conInsurancePeopleService.contractExpireMessageSend();
        //货险合同
        conInsuranceGoodsService.contractExpireMessageSend();
        //律师合同
        conLawyerService.contractExpireMessageSend();
        //船舶经纪人合同
        conBrokerService.contractExpireMessageSend();

        //船运合同长期船到期提醒
        conShippingService.contractExpireMessageSend();

    }

    /**
     * 获取配置参数
     *
     * @param enumName
     * @return
     */
    @GetMapping(value = "/listByType")
    public List<Constant> listByType(@RequestParam(value = "enumName") String enumName) {
        return konstService.listByType(enumName);
    }

    /**
     * 船运合同offhire时间段改变合同状态
     */
    @GetMapping(value = "/checkConShipState")
    public void checkConShipState() {
        List<ConShipping> conShippings = conShippingService.list();
        if (null != conShippings && conShippings.size() > 0) {
            conShippings.stream().forEach(c -> {
                if (StringUtils.isNotEmpty(c.getHireTimeValue())) {
                    ObjectMapper objectMapper = new ObjectMapper();
                    try {
                        List<HireTimeDetails> hireTimeDetails = objectMapper.readValue(c.getHireTimeValue(), new TypeReference<List<HireTimeDetails>>() {
                        });
                        if (null != hireTimeDetails && hireTimeDetails.size() > 0) {
                            hireTimeDetails.stream().forEach(s -> {
                                Date date = new Date();
                                Date start = DateUtils.parseDate(s.getStart());
                                Date end = DateUtils.parseDate(s.getEnd());
                                if (start.before(date) && date.before(end)) {
                                    c.setStatus(6);
                                    conShippingService.updateById(c);
                                }
                            });
                        }
                    } catch (JsonProcessingException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
        }
    }
}
